package com.unit5;

import com.unit2.pojo.Stu;
import com.unit5.pojo.Student;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 * @author 李琦 on 2021/8/13.
 * @version java version "16.0.1" 2021-04-20
 * 对象输入流:ObjectInputStream--将本地文件中的对象读出
 * 对象输出流:ObjectOutputStream--将对象信息写入到本地文件中
 */
public class test8 {
    public static void main(String[] args) {
        Student stu1=new Student("李琦", 22, "篮球");
        Student stu2=new Student("李琦琦", 23, "乒乓球");
        Student stu3=new Student("李琦琦琦", 24, "足球");
        Student stu4=new Student("李琦琦琦琦", 25, "棒球");
        ArrayList<Student> al=new ArrayList<Student>();

        al.add(stu1);
        al.add(stu2);
        al.add(stu3);
        al.add(stu4);

        ObjectOutputStream oos=null;
        try {
            oos=new ObjectOutputStream(new FileOutputStream("E:\\padata\\stu.txt"));
            oos.writeObject(al);//写入对象集合
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
